//
//  RegisteredViewController.m
//  BestPurchase
//
//  Created by ZSMAC on 2018/10/10.
//  Copyright © 2018年 张帅. All rights reserved.
//

#import "RegisteredViewController.h"

@interface RegisteredViewController ()
@property (weak, nonatomic) IBOutlet UIButton *backBtn;
@property (weak, nonatomic) IBOutlet UITextField *accountField;
@property (weak, nonatomic) IBOutlet UITextField *VerificationCodeField;
@property (weak, nonatomic) IBOutlet UIButton *getVerificationCodeBtn;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UITextField *PasswordFieldNew;
@property (weak, nonatomic) IBOutlet UIButton *registeredBtn;

@end

@implementation RegisteredViewController
- (id)init {
    self=[super init];
    if (self) {
        self.isRegistered=YES;
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated {
    if (self.navigationController.navigationBarHidden) {
        self.navigationController.navigationBarHidden=NO;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self UIConfiguer];
}
- (void)UIConfiguer {
    if (self.isRegistered) {
        self.title=@"注册";
        [self.registeredBtn setTitle:@"注册" forState:UIControlStateNormal];
    } else {
        self.title=@"重设密码";
        [self.registeredBtn setTitle:@"重设密码" forState:UIControlStateNormal];
    }
    
    self.registeredBtn.layer.cornerRadius=4;
    self.registeredBtn.layer.masksToBounds=YES;
    
    self.getVerificationCodeBtn.layer.cornerRadius=4;
    self.getVerificationCodeBtn.layer.masksToBounds=YES;
    
    self.accountField.clearButtonMode=UITextFieldViewModeAlways;
    self.passwordField.clearButtonMode=UITextFieldViewModeAlways;
    self.PasswordFieldNew.clearButtonMode=UITextFieldViewModeAlways;
    self.VerificationCodeField.clearButtonMode=UITextFieldViewModeAlways;

}
#pragma mark 获取验证码
- (IBAction)getVerificationCodeBtnClicked:(id)sender {
    [ZSProgressHUD showHUDShowText:@"获取验证码..."];
    if ( self.accountField.text.length!=0) {
        //请求验证码
        [BmobSMS requestSMSCodeInBackgroundWithPhoneNumber:self.accountField.text andTemplate:@"优购短信模板1" resultBlock:^(int msgId, NSError *error) {
            if (error) {
                [ZSProgressHUD showDpromptText:error.domain];
                NSLog(@"请求验证码错误 %@",error);
            } else {
                [ZSProgressHUD showDpromptText:@"获取验证码成功!"];
                //获得smsID
                NSLog(@"请求验证码成功 ID：%d",msgId);
            }
        }];
    }
}

#pragma mark 注册 1
- (IBAction)registeredBtnClicked:(id)sender {
    self.view.userInteractionEnabled=NO;
    [ZSProgressHUD showHUDShowText:@"加载中..."];
    if (self.isRegistered) {//注册
        @WeakObj(self);
        //验证码 验证
        [BmobSMS verifySMSCodeInBackgroundWithPhoneNumber:self.accountField.text andSMSCode:self.VerificationCodeField.text resultBlock:^(BOOL isSuccessful, NSError *error) {
            if (isSuccessful) {
                NSLog(@"%@",@"验证成功，可执行用户请求的操作");
                [selfWeak registered];
            } else {
                self.view.userInteractionEnabled=YES;
                [ZSProgressHUD showDpromptText:@"验证码不正确"];
                NSLog(@"%@",error);
            }
        }];
    } else {
        [self changePassword];//重设密码
    }

}
#pragma mark 注册 2
- (void)registered {
    NSString * account= self.accountField.text;
    NSString * password= self.passwordField.text;
    BmobUser *bUser = [[BmobUser alloc] init];
    [bUser setUsername:account];
    [bUser setPassword:password];
    @WeakObj(self);
    [bUser signUpInBackgroundWithBlock:^ (BOOL isSuccessful, NSError *error){
        if (isSuccessful){
            [ZSProgressHUD showSuccessfulAnimatedText:@"注册成功!"];
           selfWeak.view.userInteractionEnabled=YES;
            [selfWeak performSelector:@selector(backClicked:) withObject:nil afterDelay:1];
            NSLog(@"注册成功 %@",[BmobUser getCurrentUser]);
        } else {
            [ZSProgressHUD showErrorAnimatedText:@"注册失败!"];
            selfWeak.view.userInteractionEnabled=YES;

            NSLog(@"%@",error);
        }
    }];
}
#pragma mark 修改密码
- (void)changePassword {
    NSString * smsCode = self.VerificationCodeField.text;
    NSString * newPassword = self.PasswordFieldNew.text;
    @WeakObj(self);
    [BmobUser resetPasswordInbackgroundWithSMSCode:smsCode andNewPassword:newPassword block:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            selfWeak.view.userInteractionEnabled=YES;
            [ZSProgressHUD showSuccessfulAnimatedText:@"密码重设成功!"];
            [selfWeak performSelector:@selector(backClicked:) withObject:nil afterDelay:1];
        } else {
            [ZSProgressHUD showErrorAnimatedText:@"密码重设失败!"];
            selfWeak.view.userInteractionEnabled=YES;
        }
    }];
}

- (IBAction)backClicked:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
